package ils.web.action;

import com.opensymphony.xwork2.Preparable;
import ils.domain.issue.model.Issue;
import ils.domain.model.User;
import ils.services.IssueService;
import ils.services.MasterDataService;
import ils.util.SpringUtil;
import ils.util.Util;
import ils.web.model.MyIssue;
import ils.web.model.WBook;
import ils.web.model.WReview;
import ils.services.SearchService;
import ils.services.UgcService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class BookDetailsAction extends ActionSupport implements SessionAware, Preparable
{
    private Map<String, Object> session;
    private int bookId;
    private WBook book;
    private List<WReview> reviews;
    private SearchService searchService;
    private UgcService ugcService;
    private int firstResult;
    private int userId;
    private WReview ownReview;
    private List<MyIssue> issues;
    private IssueService issueService;
    private MasterDataService masterDataService;

    public String execute() throws Exception
    {
        userId = (Integer)session.get("userId");
        book = searchService.getBookDetails(bookId);
        reviews = ugcService.getReviewsForBook(bookId, firstResult);
        if(reviews.size() < UgcService.REVIEWS_BOOK_PAGE_SIZE)
        {
            firstResult = -1;
        }else
        {
            firstResult += UgcService.REVIEWS_BOOK_PAGE_SIZE;
        }
        if(reviews.size() == 0) reviews = null;
        ownReview = ugcService.getReviewUserBook(userId, bookId);
        if(reviews != null)
        {
            Iterator<WReview> i = reviews.iterator();
            while(i.hasNext())
            {
                WReview review = i.next();
                if(review.getUserId() == userId) i.remove();
            }
        }
        issues = getIssueDetails(issueService.getIssuesForABook(bookId, 0, 10));
        return SUCCESS;
    }

    private List<MyIssue> getIssueDetails(List<Issue> issues)
    {
        if(issues == null || issues.size() == 0) return null;
        List<Integer> userIds = new ArrayList<Integer>();
        for(Issue issue : issues)
        {
            userIds.add(issue.getUserId());
        }
        Map<Integer, User> users = masterDataService.getUserDetails(userIds);
        List<MyIssue> myIssueList = new ArrayList<>();
        for(Issue issue : issues)
        {
            MyIssue myIssue = new MyIssue();
            Util.copy(myIssue, issue);
            User user = users.get(issue.getUserId());
            //myIssue.setUserName(user.getFirstName() + " " + user.getLastName());
            myIssueList.add(myIssue);
        }
        return myIssueList;
    }

    @Override
    public void setSession(Map<String, Object> arg0)
    {
        this.session = arg0;
    }

    public int getBookId()
    {
        return bookId;
    }

    public void setBookId(int bookId)
    {
        this.bookId = bookId;
    }

    public WBook getBook()
    {
        return book;
    }

    public void setBook(WBook book)
    {
        this.book = book;
    }

    public void setSearchService(SearchService searchService)
    {
        this.searchService = searchService;
    }

    public List<WReview> getReviews()
    {
        return reviews;
    }

    public void setReviews(List<WReview> reviews)
    {
        this.reviews = reviews;
    }

    public void setUgcService(UgcService ugcService)
    {
        this.ugcService = ugcService;
    }

    public void setIssueService(IssueService issueService) {
        this.issueService = issueService;
    }

    public int getFirstResult()
    {
        return firstResult;
    }

    public void setFirstResult(int firstResult)
    {
        this.firstResult = firstResult;
    }

    public int getUserId()
    {
        return userId;
    }

    public void setUserId(int userId)
    {
        this.userId = userId;
    }

    public WReview getOwnReview()
    {
        return ownReview;
    }

    public void setOwnReview(WReview ownReview)
    {
        this.ownReview = ownReview;
    }

    public List<MyIssue> getIssues() {
        return issues;
    }

    public void setIssues(List<MyIssue> issues) {
        this.issues = issues;
    }

    @Override
    public void prepare() throws Exception {
        ugcService = (UgcService) SpringUtil.getApplicationContext().getBean("ugcService");
        searchService = (SearchService)SpringUtil.getApplicationContext().getBean("searchService");
        issueService = (IssueService)SpringUtil.getApplicationContext().getBean("issueService");
        masterDataService = (MasterDataService)SpringUtil.getApplicationContext().getBean("masterDataService");
    }
}
